package hsb.ruoyi.common.utils;


import hsb.ruoyi.common.core.text.Convert;
import io.vertx.core.MultiMap;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;

import javax.enterprise.inject.spi.CDI;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 客户端工具类
 *
 * @author ruoyi
 */
public class ServletUtils {
    /**
     * 获取String参数
     */
    public static String getParameter(HttpServerRequest request, String name) {
        return request.getParam(name);
    }

    /**
     * 获取String参数
     */
    public static String getParameter(HttpServerRequest request, String name, String defaultValue) {
        return Convert.toStr(getParameter(request, name), defaultValue);
    }

    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(HttpServerRequest request, String name) {
        return Convert.toInt(getParameter(request, name));

    }

    /**
     * 获取Integer参数
     */
    public static Integer getParameterToInt(HttpServerRequest request, String name, Integer defaultValue) {
        return Convert.toInt(getParameter(request, name), defaultValue);
    }

    /**
     * 获取Boolean参数
     */
    public static Boolean getParameterToBool(HttpServerRequest request, String name) {
        return Convert.toBool(getParameter(request, name));
    }

    /**
     * 获取Boolean参数
     */
    public static Boolean getParameterToBool(HttpServerRequest request, String name, Boolean defaultValue) {
        return Convert.toBool(getParameter(request, name), defaultValue);
    }

    /**
     * 获取request
     */
    public static HttpServerRequest getRequest() {
        return CDI.current().select(HttpServerRequest.class).get();
    }

//    /**
//     * 获取response
//     */
//    public static Http getResponse() {
//        return getRequestAttributes().getResponse();
//    }

//    /**
//     * 获取session
//     */
//    public static HttpSession getSession() {
//        return getRequest().getSession();
//    }

//    public static ServletRequestAttributes getRequestAttributes() {
//     //   RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
//    //    return (ServletRequestAttributes) attributes;
//        throw new RuntimeException("12321");
//    }

    /**
     * 将字符串渲染到客户端
     *
     * @param response 渲染对象
     * @param string   待渲染的字符串
     */
    public static void renderString(HttpServerResponse response, String string) {
        Buffer buffer = Buffer.buffer(string);
        response.setStatusCode(200);
        MultiMap headers = response.headers();
        headers.add("Content-Type", "application/json;charset=utf-8");
        headers.add("Content-Length", ""+buffer.length());
        response.write(buffer);
        response.close();

    }

    /**
     * 是否是Ajax异步请求
     *
     * @param request
     */
    public static boolean isAjaxRequest(HttpServerRequest request) {
        String accept = request.getHeader("accept");
        if (accept != null && accept.contains("application/json")) {
            return true;
        }

        String xRequestedWith = request.getHeader("X-Requested-With");
        if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
            return true;
        }

       // String uri = request.getRequestURI();
        String uri =  request.absoluteURI();


        if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
            return true;
        }

        String ajax = request.getParam("__ajax");
        return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
    }

    /**
     * 内容编码
     *
     * @param str 内容
     * @return 编码后的内容
     */
    public static String urlEncode(String str) {
        return URLEncoder.encode(str, StandardCharsets.UTF_8);
    }

    /**
     * 内容解码
     *
     * @param str 内容
     * @return 解码后的内容
     */
    public static String urlDecode(String str) {
        return URLDecoder.decode(str, StandardCharsets.UTF_8);
    }
}
